package com.derbysoft.lowcostadapter.dswitch3.translate.impl;

import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailCriteriaDTO;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailRQ;
import com.derbysoft.dswitch.dto.hotel.avail.RoomStayCandidateDTO;
import com.derbysoft.dswitch.dto.hotel.reservation.HotelReservationDTO;
import com.derbysoft.dswitch.dto.hotel.reservation.ReservationRoomStayDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelAvailRequest;
import com.derbysoft.dswitch.remote.hotel.dto.HotelReservationRequest;
import com.derbysoft.lowcostadapter.dswitch3.translate.Translator;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * Created by: jason
 * Date: 2012-06-28
 */
@Component("reservationToAvailTranslator")
public class ReservationToAvailTranslator extends AbstractDSwitch3Translator
        implements Translator<HotelReservationRequest, HotelAvailRequest, Void> {
    @Override
    public HotelAvailRequest translate(HotelReservationRequest reservationRequest, Void aVoid) {
        HotelAvailRequest hotelAvailRequest = new HotelAvailRequest();
        hotelAvailRequest.setHeader(reservationRequest.getHeader());
        HotelAvailRQ hotelAvailRQ = new HotelAvailRQ();
        hotelAvailRQ.setAvailCriteria(createAvailCriteria(reservationRequest));
        hotelAvailRequest.setHotelAvailRQ(hotelAvailRQ);
        return hotelAvailRequest;
    }

    private HotelAvailCriteriaDTO createAvailCriteria(HotelReservationRequest reservationRequest) {
        HotelReservationDTO hotelReservation = reservationRequest.getHotelReservationRQ().getHotelReservation();
        ReservationRoomStayDTO reservationRoomStayDTO = hotelReservation.getRoomStaysList().get(0);
        HotelAvailCriteriaDTO hotelAvailCriteriaDTO = new HotelAvailCriteriaDTO();
        hotelAvailCriteriaDTO.setLanguage(hotelReservation.getLanguage());
        hotelAvailCriteriaDTO.setStayDateRange(reservationRoomStayDTO.getStayDateRange());
        ArrayList<String> hotelCodes = new ArrayList<String>();
        hotelCodes.add(hotelReservation.getHotelCode());
        hotelAvailCriteriaDTO.setHotelCodesList(hotelCodes);
        hotelAvailCriteriaDTO.setRoomStayCandidate(createRoomStayCandidate(reservationRoomStayDTO));
        return hotelAvailCriteriaDTO;
    }

    private RoomStayCandidateDTO createRoomStayCandidate(ReservationRoomStayDTO reservationRoomStayDTO) {
        RoomStayCandidateDTO roomStayCandidateDTO = new RoomStayCandidateDTO();
        roomStayCandidateDTO.setChildCount(reservationRoomStayDTO.getChildCount());
        roomStayCandidateDTO.setAdultCount(reservationRoomStayDTO.getAdultCount());
        roomStayCandidateDTO.setNumberOfUnits(reservationRoomStayDTO.getNumberOfUnits());
        return roomStayCandidateDTO;
    }
}
